﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BySoft.Server.Common.Enum;
using BySoft.Server.Business;
using BySoft.Web.Utility;
using BySoft.Server.Common.Entity;
using BySoft.Server.Consts;
using BySoft.Server.Common;
using System.Data;
using OneSoft.EntityCore;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.IO;
using System.Text;


namespace TaiChySystem.Web.Site.ComPage
{
    public partial class TestUpLoad : System.Web.UI.Page
    {
        private DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("AttachmentID");//0
            dt.Columns.Add("FileType");//0
            dt.Columns.Add("FileSize");//0
            dt.Columns.Add("UpDate");
            dt.Columns.Add("FileURL");
            dt.Columns.Add("FileName");
            dt.Columns.Add("FormType");
            dt.Columns.Add("Status");
            dt.Columns.Add("FormID");
            return dt;
        }

        protected DataTable DtUpLoadPicDetail
        {
            get
            {
                if (Session["DtUpLoadPicDetail"] != null)
                {
                    return (DataTable)Session["DtUpLoadPicDetail"];
                }
                else
                {
                    return CreateDT();
                }
            }
            set
            {
                Session["DtUpLoadPicDetail"] = value;
            }
        }
      
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["PicMap"] = null;
                DtUpLoadPicDetail = null;
                //Session["DtPicDetail"] = null;
            }
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["PicMap"] == null)
                {
                    UIHelper.AlertMessage(this.Page, "未找到上传路径！");
                    return;
                }
                string strMap = Session["PicMap"].ToString().Trim();
                string strFileName = Server.MapPath("~/Files/UpLoadImage/" + strMap);
                DirectoryInfo dir = new DirectoryInfo(strFileName);
                FileInfo[] finfo = dir.GetFiles();
                DataTable dtTemp = DtUpLoadPicDetail.Clone();
                for (int i = 0; i < finfo.Length; i++)
                {
                    DataRow dr = dtTemp.NewRow();
                    dr["FileType"] = finfo[i].Extension;
                    dr["FileSize"] = finfo[i].Length;
                    dr["UpDate"] = DateTime.Now;
                    dr["FileURL"] =  strMap + "/" + finfo[i].Name;
                    dr["FileName"] = Path.GetFileName(finfo[i].Name);
                    dr["FormType"] = "PartsPic";
                    dr["Status"] = "1";
                    dtTemp.Rows.Add(dr);
                }
                DtUpLoadPicDetail = dtTemp.Copy();
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>CloseTopDialog();</script>");
                
            }
            catch (Exception ex) 
            {
                //UIHelper.AlertMessage(this.Page, FomatString(ex.Message.ToString()));
            }
        }

        /// <summary>
        ///   err string格式化 去除"\r \n"
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public string FomatString(string str)
        {
            if (str == null || str == "")
                return "";
            else return str.ToString().Replace("\r", "").Replace("\n", "").Trim();
        }
    }
}